package tmp

import "testing"

func createFile(t *testing.T, filename string, ft *FileTest) {
	f, err := CreateFile(filename, CLOBBER|NETCDF4)
	if err != nil {
		t.Fatalf("Create failed: %v\n", err)
	}
	dims := make([]Dim, len(ft.DimNames))
	for i, name := range ft.DimNames {
		if dims[i], err = f.AddDim(name, ft.DimLens[i]); err != nil {
			t.Fatalf("PutDim failed: %v\n", err)
		}
	}
	v, err := f.AddVar(ft.VarName, ft.DataType, dims)
	if err != nil {
		t.Fatalf("PutVar failed: %v\n", err)
	}
	ft.putAttrs(t, v)

	n, err := v.Len()
	if err != nil {
		t.Fatalf("Var.Len failed: %v\n", err)
	}
	switch ft.DataType {
	default:
		t.Fatalf("unexpected type %v\n", ft.DataType)
	case UINT64:
		err = testWriteUint64s(v, n)
	case INT64:
		err = testWriteInt64s(v, n)
	case DOUBLE:
		err = testWriteFloat64s(v, n)
	case UINT:
		err = testWriteUint32s(v, n)
	case INT:
		err = testWriteInt32s(v, n)
	case FLOAT:
		err = testWriteFloat32s(v, n)
	case USHORT:
		err = testWriteUint16s(v, n)
	case SHORT:
		err = testWriteInt16s(v, n)
	case UBYTE:
		err = testWriteUint8s(v, n)
	case BYTE:
		err = testWriteInt8s(v, n)
	case CHAR:
		err = testWriteBytes(v, n)
	}
	if err != nil {
		t.Errorf("writing data failed: %v\n", err)
	}
	if err := f.Close(); err != nil {
		t.Fatalf("Close failed: %v\n", err)
	}
}
